﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Threading;
using System.Windows;
using Autofac;
using MadMediaManager.Core.DependencyInjectionSupport;
using MadMediaManager.WPFSupport;
using MadMediaManager.WPFSupport.Interfaces.Services;
using MadMediaManager.WPFSupport.Interfaces.Views;
using MadMediaManager.WPFSupport.Services;
using MadMediaManager.WPFSupport.Services.Threading;
using Sanbox.Interfaces.Views;
using Sanbox.ViewModels;
using Sanbox.Views;

namespace Sanbox
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : Application
    {
        protected override void OnStartup(StartupEventArgs e)
        {
            base.OnStartup(e);

            //AppServiceContainer.Init(new ContainerConfigurator());
            //IMainView mainWindow = AppServiceContainer.Get<IMainView>();
            //mainWindow.ViewModel = AppServiceContainer.Get<MainViewModel>();
            //mainWindow.Show();
            //Thread.Sleep(500);
            //AppServiceContainer.Get<IViewManager>().TransitionToHome();
            MainWindowDesignTest w = new MainWindowDesignTest();
            w.Show();
        }
    }

    public class ContainerConfigurator : IContainerConfigurator<IContainer>
    {
        public IContainer Build()
        {
            var mainContainerBuilder = new ContainerBuilder();

            mainContainerBuilder.RegisterType<ViewManagerTestWindow>()
                .AsImplementedInterfaces()
                .SingleInstance();

            mainContainerBuilder.Register(c => new HomeView(c.Resolve<HomeViewModel>())).As<IHomeView>();

            mainContainerBuilder.Register(c => new ViewFactory(c)).As<IViewFactory>()
                .SingleInstance();

            mainContainerBuilder.Register(c => new HomeViewModel());

            mainContainerBuilder.Register(c => new MainViewModel(c.Resolve<IViewManager>()))
                .SingleInstance();

            mainContainerBuilder.Register(c => new EventPublisher(new DispatcherWrapper())).As<IEventPublisher>().SingleInstance();

            mainContainerBuilder
                .Register(c => new ViewManager(c.Resolve<IViewFactory>(), c.Resolve<IEventPublisher>(), 
                    new Transitioner(), c.Resolve<IContentRegionOwner>())
                    .SetHomeView(new ViewKey<IHomeView>(null), TransitionType.Fade))
                .As<IViewManager>()
                .SingleInstance();

            return mainContainerBuilder.Build();
        }
    }

    public class ViewFactory : IViewFactory
    {
        private IComponentContext _container;
        public ViewFactory(IComponentContext container)
        {
            _container = container;
        }

        public TView Get<TView>() where TView : INavigableView
        {
            return _container.Resolve<TView>();
        }
    }
}
